<template>
  <div class="articles-management">
    <!-- 工具栏区域 -->
    <div class="toolbar">
      <div class="search-filter">
        <input 
          type="text" 
          v-model="searchQuery" 
          class="admin-form-input search-input" 
          placeholder="搜索文章标题或内容..."
          @input="handleSearch"
        />
        <select v-model="statusFilter" class="admin-form-input filter-select" @change="handleFilterChange">
          <option value="all">所有状态</option>
          <option value="published">已发布</option>
          <option value="draft">草稿</option>
          <option value="archived">已归档</option>
        </select>
        <select v-model="categoryFilter" class="admin-form-input filter-select" @change="handleFilterChange">
          <option value="all">所有分类</option>
          <option value="news">新闻动态</option>
          <option value="guide">使用指南</option>
          <option value="knowledge">环保知识</option>
          <option value="event">活动公告</option>
        </select>
      </div>
      <div class="actions">
        <router-link to="/admin/articles/new" class="admin-btn admin-btn-primary">
          <i class="icon-plus"></i> 新建文章
        </router-link>
        <button class="admin-btn" @click="exportData">
          <i class="icon-export"></i> 导出数据
        </button>
      </div>
    </div>

    <!-- 文章列表区域 -->
    <div class="table-container">
      <table class="admin-table articles-table">
        <thead>
          <tr>
            <th style="width: 60px">ID</th>
            <th style="width: 80px">封面</th>
            <th style="width: 200px">标题</th>
            <th style="width: 100px">作者</th>
            <th style="width: 100px">分类</th>
            <th style="width: 100px">状态</th>
            <th style="width: 150px">发布时间</th>
            <th style="width: 80px">阅读量</th>
            <th style="width: 150px">操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="article in displayedArticles" :key="article.id">
            <td>{{ article.id }}</td>
            <td>
              <div class="article-cover">
                <img v-if="article.cover" :src="article.cover" alt="文章封面" />
                <div v-else class="no-cover">无封面</div>
              </div>
            </td>
            <td class="article-title">{{ article.title }}</td>
            <td>{{ article.author }}</td>
            <td>
              <span :class="getCategoryClass(article.category)">{{ getCategoryName(article.category) }}</span>
            </td>
            <td>
              <span :class="getStatusClass(article.status)">{{ getStatusName(article.status) }}</span>
            </td>
            <td>{{ article.publishedAt }}</td>
            <td>{{ article.views }}</td>
            <td class="actions-cell">
              <router-link :to="`/admin/articles/${article.id}`" class="action-btn edit-btn">
                编辑
              </router-link>
              <button class="action-btn delete-btn" @click="showDeleteConfirm(article)">
                删除
              </button>
            </td>
          </tr>
          <tr v-if="displayedArticles.length === 0">
            <td colspan="9" class="empty-data">
              没有找到符合条件的文章
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- 分页区域 -->
    <div class="pagination" v-if="totalPages > 1">
      <button 
        class="page-btn" 
        :disabled="currentPage === 1"
        @click="goToPage(currentPage - 1)"
      >
        上一页
      </button>
      <div class="page-info">
        {{ currentPage }} / {{ totalPages }} 页 (共 {{ filteredArticles.length }} 条)
      </div>
      <button 
        class="page-btn" 
        :disabled="currentPage === totalPages"
        @click="goToPage(currentPage + 1)"
      >
        下一页
      </button>
    </div>

    <!-- 删除确认模态框 -->
    <div class="modal-overlay" v-if="showDeleteModal" @click="cancelDelete">
      <div class="modal-content" @click.stop>
        <h3 class="modal-title">确认删除</h3>
        <p class="modal-message">
          您确定要删除文章 <strong>"{{ articleToDelete?.title }}"</strong> 吗？此操作无法撤销。
        </p>
        <div class="modal-buttons">
          <button class="admin-btn" @click="cancelDelete">取消</button>
          <button class="admin-btn admin-btn-danger" @click="confirmDelete">确认删除</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'AdminArticles',
  data() {
    return {
      articles: [
        {
          id: 1,
          title: '每日低碳生活：5个简单但有效的节能方法',
          author: '环保小编',
          category: 'knowledge',
          status: 'published',
          cover: 'https://via.placeholder.com/60x40',
          publishedAt: '2023-05-15 10:30',
          views: 2456
        },
        {
          id: 2,
          title: '本周碳积分兑换活动公告',
          author: '系统管理员',
          category: 'event',
          status: 'published',
          cover: 'https://via.placeholder.com/60x40',
          publishedAt: '2023-05-12 14:20',
          views: 1890
        },
        {
          id: 3,
          title: '夏季节能降耗指南：如何减少空调用电',
          author: '环保专家',
          category: 'guide',
          status: 'draft',
          cover: null,
          publishedAt: '未发布',
          views: 0
        },
        {
          id: 4,
          title: 'CarbonSpark平台更新：新增碳足迹计算功能',
          author: '系统管理员',
          category: 'news',
          status: 'published',
          cover: 'https://via.placeholder.com/60x40',
          publishedAt: '2023-05-10 09:15',
          views: 3240
        },
        {
          id: 5,
          title: '了解碳中和：从个人到企业的行动指南',
          author: '碳中和专家',
          category: 'knowledge',
          status: 'archived',
          cover: 'https://via.placeholder.com/60x40',
          publishedAt: '2023-04-22 16:45',
          views: 1275
        }
      ],
      searchQuery: '',
      statusFilter: 'all',
      categoryFilter: 'all',
      currentPage: 1,
      pageSize: 10,
      showDeleteModal: false,
      articleToDelete: null
    }
  },
  computed: {
    filteredArticles() {
      return this.articles.filter(article => {
        // 搜索过滤
        const matchesSearch = article.title.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
                           (article.content && article.content.toLowerCase().includes(this.searchQuery.toLowerCase()));
        
        // 状态过滤
        const matchesStatus = this.statusFilter === 'all' || article.status === this.statusFilter;
        
        // 分类过滤
        const matchesCategory = this.categoryFilter === 'all' || article.category === this.categoryFilter;
        
        return matchesSearch && matchesStatus && matchesCategory;
      });
    },
    displayedArticles() {
      const startIndex = (this.currentPage - 1) * this.pageSize;
      const endIndex = startIndex + this.pageSize;
      return this.filteredArticles.slice(startIndex, endIndex);
    },
    totalPages() {
      return Math.ceil(this.filteredArticles.length / this.pageSize);
    }
  },
  methods: {
    handleSearch() {
      this.currentPage = 1;
    },
    handleFilterChange() {
      this.currentPage = 1;
    },
    getCategoryName(category) {
      const categoryMap = {
        'news': '新闻动态',
        'guide': '使用指南',
        'knowledge': '环保知识',
        'event': '活动公告'
      };
      return categoryMap[category] || '未知分类';
    },
    getCategoryClass(category) {
      const classMap = {
        'news': 'admin-tag admin-tag-blue',
        'guide': 'admin-tag admin-tag-green',
        'knowledge': 'admin-tag admin-tag-orange',
        'event': 'admin-tag admin-tag-blue'
      };
      return classMap[category] || 'admin-tag';
    },
    getStatusName(status) {
      const statusMap = {
        'published': '已发布',
        'draft': '草稿',
        'archived': '已归档'
      };
      return statusMap[status] || '未知状态';
    },
    getStatusClass(status) {
      const classMap = {
        'published': 'admin-tag admin-tag-green',
        'draft': 'admin-tag admin-tag-orange',
        'archived': 'admin-tag admin-tag-blue'
      };
      return classMap[status] || 'admin-tag';
    },
    goToPage(page) {
      if (page >= 1 && page <= this.totalPages) {
        this.currentPage = page;
      }
    },
    exportData() {
      alert('导出文章数据功能将在后续版本中实现');
    },
    showDeleteConfirm(article) {
      this.articleToDelete = article;
      this.showDeleteModal = true;
    },
    cancelDelete() {
      this.showDeleteModal = false;
      this.articleToDelete = null;
    },
    confirmDelete() {
      if (this.articleToDelete) {
        // 这里应该调用API删除文章
        const index = this.articles.findIndex(a => a.id === this.articleToDelete.id);
        if (index !== -1) {
          this.articles.splice(index, 1);
        }
        this.showDeleteModal = false;
        this.articleToDelete = null;
      }
    }
  }
}
</script>

<style scoped>
.articles-management {
  width: 100%;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 10px;
}

.search-filter {
  display: flex;
  gap: 10px;
  flex: 1;
  min-width: 300px;
}

.search-input {
  flex: 2;
  min-width: 200px;
}

.filter-select {
  flex: 1;
  min-width: 120px;
}

.actions {
  display: flex;
  gap: 10px;
}

.icon-plus, .icon-export {
  margin-right: 5px;
}

.table-container {
  width: 100%;
  overflow-x: auto;
}

.articles-table {
  min-width: 1000px;
}

.article-cover {
  width: 60px;
  height: 40px;
  overflow: hidden;
  border-radius: 4px;
  background-color: #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.article-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.no-cover {
  font-size: 12px;
  color: #999;
}

.article-title {
  font-weight: 500;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.actions-cell {
  display: flex;
  gap: 8px;
}

.action-btn {
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  border: none;
  background-color: transparent;
  transition: all 0.3s;
}

.edit-btn {
  color: #1890ff;
}

.edit-btn:hover {
  background-color: rgba(24, 144, 255, 0.1);
}

.delete-btn {
  color: #ff4d4f;
}

.delete-btn:hover {
  background-color: rgba(255, 77, 79, 0.1);
}

.empty-data {
  text-align: center;
  padding: 30px 0;
  color: #999;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  gap: 10px;
}

.page-btn {
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  border: 1px solid #d9d9d9;
  background-color: white;
  transition: all 0.3s;
}

.page-btn:disabled {
  cursor: not-allowed;
  color: rgba(0, 0, 0, 0.25);
  background-color: #f5f5f5;
  border-color: #d9d9d9;
}

.page-btn:not(:disabled):hover {
  color: #40a9ff;
  border-color: #40a9ff;
}

.page-info {
  color: rgba(0, 0, 0, 0.65);
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background-color: white;
  border-radius: 4px;
  padding: 24px;
  width: 400px;
  max-width: 90vw;
}

.modal-title {
  margin-top: 0;
  margin-bottom: 16px;
  color: rgba(0, 0, 0, 0.85);
  font-weight: 500;
}

.modal-message {
  margin-bottom: 24px;
  color: rgba(0, 0, 0, 0.65);
}

.modal-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 